import { _decorator, CCString, Component, Node, randomRangeInt } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('RandomUID')
export class RandomUID extends Component {

    @property({visible: true, readonly: true})
    _uid: string = generateUID()

    start() {
        for (let index = 0; index < 100; index++) {
            
            console.log(generateUID("name"))
        }
    }

    update(deltaTime: number) {
        
    }
}

// /**
//  * 生成唯一标识
//  * @param prefixName 前缀
//  * @returns 前缀
//  */

/**
 * 生成唯一标识
 * @param prefixName 前缀
 * @returns 返回值 = 前缀_时间戳_ID_字母
 */
export function generateUID(prefixName: string = ''): string {
    const date = new Date().getTime()
    const AToZ = 'abcdefghijklmnopqrstuvwxyz'
    const a = AToZ[randomRangeInt(0, AToZ.length)] 
    const b = AToZ[randomRangeInt(0, AToZ.length)] 
    const c = AToZ[randomRangeInt(0, AToZ.length)] 
    const d = AToZ[randomRangeInt(0, AToZ.length)] 
    const letter = a + b + c + d
    const randomID = randomRangeInt(100000, 999999)
    if (prefixName) {
        return prefixName + "_" + date + "_" + randomID + "_" + letter
    } else {
        return date + "_" + randomID + "_" + letter
    }
}